<?php
class Form_UserSubForm extends Zend_Form_SubForm 
{
   /**
    * init() routine called when Zend_Form objects are created.
    * It make sense to define form elements in this method.
    * 
    * @see    http://framework.zend.com/manual/en/zend.form.html
    * @return void
    */ 
    public function init()
    {
    	 $username = new Zend_Form_Element_Text('openid_identifier');
         $username->setLabel('username')
                  ->setAttrib('class', 'field')
                  ->setAttrib('onkeyup','checkForRange(this, 4, 100);')
                  ->setAttrib('onclick','checkForRange(this, 4, 100);')
                  ->setDescription('msgUsername')
                  ->setRequired(true)
                  ->addValidator('StringLength', true, array('min' => '4', 'max' => '100'))  
                  ->setDecorators(array(
                        array('ViewHelper', array('helper'=>'formText')),
                        array('Label', array('separator'=>' ', 'class'=>'gray labelfield')),
                        array('Errors'),
                        array('Description', array('tag' => 'span', 'class' => 'hint')),
                        array('HtmlTag', array('tag' => 'li', 'class' => 'element-group'))
                  ));
                    
         $password = new Zend_Form_Element_Password('password');
         $password->setLabel('Password:')
                  ->setAttrib('class', 'field')
                  ->setAttrib('onkeyup','checkPassword(this);')
                  ->setAttrib('onclick','checkPassword(this);')
                  ->setDescription('msgPassword')
                  ->addValidator(new Form_PasswordConfirmation())
                  ->setDecorators(array(
                        array('ViewHelper'),
                        array('Label', array('separator'=>' ', 'class'=>'gray labelfield')),
                        array('Errors'),
                        array('Description', array('tag' => 'span', 'class' => 'hint')),
                        array('HtmlTag', array('tag' => 'li', 
                                               'class' => 'element-group',
                                               'id' => 'lipassword'))
                  ));
                    
         $confirmpwd = new Zend_Form_Element_Password('confirmPassword');
         $confirmpwd->setLabel('confirm')
                  ->setAttrib('class', 'field profileConfirmPwd')
                  ->setAttrib('onkeyup','checkPassword(this);')
                  ->setAttrib('onclick','checkPassword(this);')
                  ->setDescription('msgPassword')
                  ->setDecorators(array(
                        array('ViewHelper'),
                        array('Label', array('separator'=>' ', 'class'=>'gray labelfield')),
                        array('Errors'),
                        array('Description', array('tag' => 'span', 'class' => 'hint')),
                        array('HtmlTag', array('tag' => 'li', 
                                               'class' => 'element-group',
                                               'id' => 'liconfirmPassword'))
                  ));

         // Add an openID checkbox
         $checkbox = new Zend_Form_Element_Checkbox('isOpenID');
         $checkbox->setLabel('Open ID')
                  ->setDecorators(array(
                       array('ViewHelper', array('helper'=>'formCheckbox')),
                       array('Label', array('class'=>'grey labelOpenid')),
                       array('HtmlTag', array('tag' => 'li', 'class' => 'element-group'))
                  ));
                    
         // Add hidden field
         $openid_action = new Zend_Form_Element_Hidden('openid_action');
         $openid_action->setDecorators(array('ViewHelper'));
         $openid_action->setValue('login');     

         $user_oid = new Zend_Form_Element_Hidden('user_oid');
         $user_oid->setDecorators(array('ViewHelper'));
         
         $this->addElements(array(
            $username,
            $password,
            $confirmpwd,
            $checkbox,
            $openid_action,
            $user_oid
         ));
    }
    
	/**
	 * Override validation to set password field requirement only for normal login
	 */
	public function isValid($data) {
	   if (array_key_exists('user', $data) && $data['user']['isOpenID'] == 1) {
           $this->password->setRequired(false);
           $this->confirmPassword->setRequired(false);
       } else {
           $this->password->setRequired(true);
           $this->confirmPassword->setRequired(true);
       }
       
       return parent::isValid($data);
	}
}
